package com.taozi.netty.socketio.use.classroomchat.service.socketio;

import com.corundumstudio.socketio.Configuration;
import com.corundumstudio.socketio.SocketIOServer;
import com.taozi.netty.socketio.use.classroomchat.entity.vo.education.ClassroomChatVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * description netty-SocketIo 聊天启动
 *
 * @author mingtao
 * date：2019/7/1 16:33
 */
@Component
@Slf4j
public class ChatServer implements CommandLineRunner {

    // 注入监听类
    @Resource
    private EventListenner eventListenner;

    @Resource
    private LoginChatListener loginChatListener;

    @Resource
    private GroupChatListener groupChatListener;

    @Resource
    private BanChatListener banChatListener;

    @Resource
    private ChatUserListener chatUserListener;


    @Value("${netty-socketio.hostname}")
    private String hostname;
    @Value("${netty-socketio.port}")
    private Integer port;

    @Override
    public void run(String... args) throws Exception {

        /**
         * 当CommandLineRunner中出现不可预期的异常时，会影响主线程，所以这里单独启动一个线程执行
         */
        new Thread(){
            public void run(){
                try {
                    chatStart();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }.start();

    }
    private void chatStart() throws Exception{

        Configuration config = new Configuration();
        config.setHostname(hostname);
        config.setPort(port);
        /**
         * 设置origin 解决跨域
         * 注意如果开放跨域设置，需要设置为null而不是"*"，
         * 并且前端在连接时需要加上{transports:['websocket']}
         * 如：socket = io.connect('ws://192.168.11.134:8888?userId=1&classroomId=1,{transports:['websocket']});
        **/
        config.setOrigin(null);// 注意如果开放跨域设置，需要设置为null而不是"*"，并且前端在连接时需要加上{transports:['websocket']}
        config.setWorkerThreads(100);

        SocketIOServer socketIOServer = new SocketIOServer(config);


        // 连接状态监听
        eventListenner.setServer(socketIOServer);
        socketIOServer.addListeners(eventListenner);

        // 登录聊天室监听
        loginChatListener.setServer(socketIOServer);
        socketIOServer.addEventListener("login", ClassroomChatVo.class, loginChatListener);

        // 创建课堂聊天室监听
        groupChatListener.setServer(socketIOServer);
        socketIOServer.addEventListener("groupChat", ClassroomChatVo.class, groupChatListener);

        // 课堂聊天室禁言操作监听
        banChatListener.setServer(socketIOServer);
        socketIOServer.addEventListener("ban", ClassroomChatVo.class, banChatListener);

        // 课堂聊天室在线人员监听
        chatUserListener.setServer(socketIOServer);
        socketIOServer.addEventListener("chatUser", ClassroomChatVo.class, chatUserListener);


        //启动服务
        socketIOServer.start();
        log.info("socketIOServer 启动成功。。。。。。");



    }
}
